import java.util.Stack;

public class Solution2281 {
    public int totalStrength(int[] strength) {
        long mod=1000000007;
        long[] sum=new long[strength.length+1];
        for (int i=1;i<=strength.length;i++){
            sum[i]=(sum[i-1]+strength[i-1])%mod;
        }
        long[] sum2=new long[strength.length+2];
        for (int i=1;i<=strength.length+1;i++){
            sum2[i]=(sum2[i-1]+sum[i-1])%mod;
        }
        Stack<Integer> stack=new Stack<>();
        int[] p=new int[strength.length];
        for (int i=0;i<strength.length;i++){
            while (!stack.isEmpty() && strength[i]<=strength[stack.peek()]){
                stack.pop();
            }
            if (stack.isEmpty()){
                p[i]=i;
            }else {
                p[i]=i-stack.peek()-1;
            }
            stack.push(i);
        }
        stack.clear();
        int[] q=new int[strength.length];
        for (int i=strength.length-1;i>=0;i--){
            while (!stack.isEmpty() && strength[i]<strength[stack.peek()]){
                stack.pop();
            }
            if (stack.isEmpty()){
                q[i]=strength.length-i-1;
            }else {
                q[i]=stack.peek()-i-1;
            }
            stack.push(i);
        }
        long ans=0;
        for (int i=0;i<strength.length;i++){
            long x=(sum2[i+q[i]+2]-sum2[i+1]-(q[i]+1)*sum[i]%mod)%mod;
            long y=(sum[i]*p[i]%mod-(sum2[i]-sum2[i-p[i]])%mod)%mod;
            ans=((ans+strength[i]*(((p[i]+1)*x%mod+(q[i]+1)*y%mod)%mod)%mod)%mod+mod)%mod;
        }
        return (int)ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2281().totalStrength(new int[]{1,3,1,2}));
    }
}
